package com.ruoyi.system.service.report.Impl;


import com.ruoyi.common.core.domain.JoinLevelResVo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.report.vo.*;
import com.ruoyi.system.mapper.report.StudentMapper;
import com.ruoyi.system.service.report.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

@Service
public class IStudentServiceImpl implements IStudentService{

    @Autowired
    private StudentMapper studentMapper;



    /**
     * 分页条件查询学生报告列表
     * @param studentReportLisReqVo
     * @return
     */
    @Override
    public List<StudentReportListResVo> getPatientInfoList(StudentReportLisReqVo studentReportLisReqVo) {
        return studentMapper.getPatientInfoList(studentReportLisReqVo);
    }

    /**
     * 更新学生报告
     * @param studentReportVo
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
    public void updateReport(StudentReportVo studentReportVo) {
        studentMapper.updateReport(studentReportVo);
    }

    /**
     * 查询学生患者
     * @param reportId
     * @return
     */
    @Override
    public StudentReportInfoResVo getPatientInfo(String reportId) {
        return studentMapper.getPatientInfo(reportId);
    }


    /**
     * 选择班级的树形结构
     * @return
     */
    @Override
    public HashMap<String, List<JoinLevelResVo>> classSelect() {
        // 1. 先查出所有分类数据
        List<JoinLevelResVo> list = studentMapper.selectList();
        List<JoinLevelResVo> selectVoList = templeLibTree(list);
        HashMap<String, List<JoinLevelResVo>> stringArrayListHashMap = new HashMap<>();
        stringArrayListHashMap.put("public",selectVoList);
        return stringArrayListHashMap;
    }

    /**
     * 保存修改
     * @param studentReportVo
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
    public void saveReport(StudentReportVo studentReportVo) {
        studentMapper.saveReport(studentReportVo);
    }

    /**
     * 查询随访记录
     * @param reportId
     * @return
     */
    @Override
    public List<SurveyVo> getSurvey(String reportId) {
        return studentMapper.getSurvey(reportId);
    }

    /**
     * 获取报告
     * @param reportId
     * @return
     */
    @Override
    public List<PtDetailBgReadyVo> getBgReadyTxt(String reportId) {
        return studentMapper.getBgReadyTxt(reportId);
    }

    /**
     * 获取检查所见
     * @param reportId
     * @return
     */
    @Override
    public List<PtDetailCheckSeeTxtVo> getCheckSee(String reportId) {
        return studentMapper.getCheckSee(reportId);
    }

    /**
     * 检查意见
     * @param reportId
     * @return
     */
    @Override
    public List<PtCheckOpinionVo> getCheckOpinion(String reportId) {
        return studentMapper.getCheckOpinion(reportId);
    }

    @Override
    public CompareDataVo getCompareData(String reportId) {
        return studentMapper.getCompareData(reportId);
    }

    private List<JoinLevelResVo> templeLibTree(List<JoinLevelResVo> depts)
    {
        List<JoinLevelResVo> returnList = new ArrayList<JoinLevelResVo>();
        List<Long> joinLevelResVo = new ArrayList<Long>();
        for (JoinLevelResVo dept : depts)
        {
            joinLevelResVo.add(dept.getValue());
        }
        for (Iterator<JoinLevelResVo> iterator = depts.iterator(); iterator.hasNext();)
        {
            JoinLevelResVo dept = (JoinLevelResVo) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!joinLevelResVo.contains(dept.getPrValue()))
            {
                recursionFn(depts, dept);
                returnList.add(dept);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = depts;
        }
        return returnList;
    }

    private void recursionFn(List<JoinLevelResVo> list, JoinLevelResVo t)
    {
        // 得到子节点列表
        List<JoinLevelResVo> childList = getChildList(list, t);
        t.setChildren(childList);
        for (JoinLevelResVo tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    private List<JoinLevelResVo> getChildList(List<JoinLevelResVo> list, JoinLevelResVo t)
    {
        List<JoinLevelResVo> tlist = new ArrayList<>();
        Iterator<JoinLevelResVo> it = list.iterator();
        while (it.hasNext())
        {
            JoinLevelResVo n = (JoinLevelResVo) it.next();
            if (StringUtils.isNotNull(n.getPrValue()) && n.getPrValue().longValue() == t.getValue().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    private boolean hasChild(List<JoinLevelResVo> list, JoinLevelResVo t)
    {
        return getChildList(list, t).size() > 0;
    }


}
